package com.asl.website.joinModule.controller;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.asl.website.joinModule.domain.JoinModuleDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.asl.common.annotation.Log;
import com.asl.common.core.controller.BaseController;
import com.asl.common.core.domain.AjaxResult;
import com.asl.common.enums.BusinessType;
import com.asl.website.joinModule.domain.AslJoinModule;
import com.asl.website.joinModule.service.IAslJoinModuleService;
import com.asl.common.utils.poi.ExcelUtil;
import com.asl.common.core.page.TableDataInfo;

/**
 * 加入我们模块Controller
 * 
 * @author ruoyi
 * @date 2025-05-22
 */
@RestController
@RequestMapping("/joinModule/joinModule")
public class AslJoinModuleController extends BaseController
{
    @Autowired
    private IAslJoinModuleService aslJoinModuleService;
    /**
     * 获取所有加入我们模块列表（按排序）
     */
    @GetMapping("/content")
    public AjaxResult getAllModules(
            @RequestHeader(value = "Accept-Language", defaultValue = "zh-CN") String acceptLanguage) {

        List<AslJoinModule> modules = aslJoinModuleService.selectAslJoinModuleList(new AslJoinModule());
        if (modules.isEmpty()) {
            return AjaxResult.error("未配置任何加入模块");
        }

        List<JoinModuleDTO> result = modules.stream()
                .map(module -> new JoinModuleDTO(module, acceptLanguage))
                .sorted(Comparator.comparingInt(JoinModuleDTO::getSortOrder))
                .collect(Collectors.toList());

        return AjaxResult.success(result);
    }
    /**
     * 查询加入我们模块列表
     */
    @PreAuthorize("@ss.hasPermi('joinModule:joinModule:list')")
    @GetMapping("/list")
    public TableDataInfo list(AslJoinModule aslJoinModule)
    {
        startPage();
        List<AslJoinModule> list = aslJoinModuleService.selectAslJoinModuleList(aslJoinModule);
        return getDataTable(list);
    }

    /**
     * 导出加入我们模块列表
     */
    @PreAuthorize("@ss.hasPermi('joinModule:joinModule:export')")
    @Log(title = "加入我们模块", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AslJoinModule aslJoinModule)
    {
        List<AslJoinModule> list = aslJoinModuleService.selectAslJoinModuleList(aslJoinModule);
        ExcelUtil<AslJoinModule> util = new ExcelUtil<AslJoinModule>(AslJoinModule.class);
        util.exportExcel(response, list, "加入我们模块数据");
    }

    /**
     * 获取加入我们模块详细信息
     */
    @PreAuthorize("@ss.hasPermi('joinModule:joinModule:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(aslJoinModuleService.selectAslJoinModuleById(id));
    }

    /**
     * 新增加入我们模块
     */
    @PreAuthorize("@ss.hasPermi('joinModule:joinModule:add')")
    @Log(title = "加入我们模块", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AslJoinModule aslJoinModule)
    {
        return toAjax(aslJoinModuleService.insertAslJoinModule(aslJoinModule));
    }

    /**
     * 修改加入我们模块
     */
    @PreAuthorize("@ss.hasPermi('joinModule:joinModule:edit')")
    @Log(title = "加入我们模块", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AslJoinModule aslJoinModule)
    {
        return toAjax(aslJoinModuleService.updateAslJoinModule(aslJoinModule));
    }

    /**
     * 删除加入我们模块
     */
    @PreAuthorize("@ss.hasPermi('joinModule:joinModule:remove')")
    @Log(title = "加入我们模块", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(aslJoinModuleService.deleteAslJoinModuleByIds(ids));
    }
}
